package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.utils.PageResult;
import com.woniuxy.entity.Blog;
import com.woniuxy.mapper.BlogMapper;
import com.woniuxy.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;

    @Override
    public PageResult findBlogByTypeId(Integer pageNum, Integer pageSize,Integer id) {
        //设置当前页和每页记录数
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper方法
        List<Blog> list = blogMapper.findBlogByTypeId(id);
        //创建PageInfo对象
        PageInfo<Blog> pageInfo = new PageInfo<>(list);
        //封装响应数据
        PageResult ps = new PageResult(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
        return ps;
    }

    @Override
    public Integer publishCount(Integer userId) {
        return blogMapper.publishCount(userId);
    }

    @Override
    public Integer getBlogCount(Integer itemId) {
        return blogMapper.getBlogCount(itemId);
    }

    @Override
    public PageResult search(Integer pageNum, Integer pageSize, String blogName) {
        //设置当前页和每页记录数
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper方法
        List<Blog> list = blogMapper.search(blogName);
        //创建PageInfo对象
        PageInfo<Blog> pageInfo = new PageInfo<>(list);
        //封装响应数据
        PageResult ps = new PageResult(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
        return ps;
    }

    @Override
    public PageResult findUserBlog(Integer pageNum, Integer pageSize, Integer userId) {
        //设置当前页和每页记录数
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper方法
        List<Blog> list = blogMapper.findUserBlog(userId);
        //创建PageInfo对象
        PageInfo<Blog> pageInfo = new PageInfo<>(list);
        //封装响应数据
        PageResult ps = new PageResult(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
        return ps;
    }

    @Override
    public PageResult findUserCollectBlog(Integer pageNum, Integer pageSize, Integer userId) {
        //设置当前页和每页记录数
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper方法
        List<Blog> list = blogMapper.findUserCollectBlog(userId);
        //创建PageInfo对象
        PageInfo<Blog> pageInfo = new PageInfo<>(list);
        //封装响应数据
        PageResult ps = new PageResult(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
        return ps;
    }

    @Override
    public int add(Blog blog) {
        blogMapper.add(blog);
        return blog.getId();
    }

    @Override
    public int addVideo(Blog blog) {
        blogMapper.addVideo(blog);
        return blog.getId();
    }
}
